﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace CPPEI.WebApiTool
{
    public class WebApiRefBuilder
    {
        StringBuilder _stringBuilder = new StringBuilder();

        List<Assembly> _assemblyList;

        public WebApiRefBuilder(List<Assembly> assemblyList)
        {
            _assemblyList = assemblyList;
        }

        public void Build(string filePath)
        {
            if(File.Exists(filePath))
            {
                XmlDocument xDoc = new XmlDocument();
                xDoc.Load(filePath);

                XmlNode xWebApi = xDoc.SelectSingleNode("./WebApi");
                if (xWebApi == null)
                {
                    xDoc.RemoveAll();

                    xWebApi = xDoc.CreateElement("WebApi");
                    xDoc.AppendChild(xWebApi);
                }

                XmlNode xModules = xDoc.SelectSingleNode("./WebApi/Modules");
                if(xModules == null)
                {
                    xModules = xDoc.CreateElement("Modules");
                    foreach (var assembly in _assemblyList)
                    {
                        XmlElement xModule = xDoc.CreateElement("Module");

                        XmlAttribute xattrAssembly = xDoc.CreateAttribute("Assembly");
                        xattrAssembly.Value = assembly.FullName;
                        xModule.Attributes.Append(xattrAssembly);

                        xModules.AppendChild(xModule);
                    }

                    xWebApi.AppendChild(xModules);
                }
                else
                {
                    xModules.RemoveAll();
                    foreach (var assembly in _assemblyList)
                    {
                        XmlElement xModule = xDoc.CreateElement("Module");

                        XmlAttribute xattrAssembly = xDoc.CreateAttribute("Assembly");
                        xattrAssembly.Value = assembly.FullName;
                        xModule.Attributes.Append(xattrAssembly);

                        xModules.AppendChild(xModule);
                    }
                }

                xDoc.Save(filePath);
            }
            else
            {
                XmlDocument xDoc = new XmlDocument();

                XmlNode xWebApi = xDoc.SelectSingleNode("./WebApi");
                if (xWebApi == null)
                {
                    xDoc.RemoveAll();

                    xWebApi = xDoc.CreateElement("WebApi");
                    xDoc.AppendChild(xWebApi);
                }

                XmlElement xWebApiList = xDoc.CreateElement("Modules");

                foreach (var assembly in _assemblyList)
                {
                    XmlElement xModule = xDoc.CreateElement("Module");

                    XmlAttribute xattrAssembly = xDoc.CreateAttribute("Assembly");
                    xattrAssembly.Value = assembly.FullName;
                    xModule.Attributes.Append(xattrAssembly);

                    xWebApiList.AppendChild(xModule);
                }
                xWebApi.AppendChild(xWebApiList);

                xDoc.Save(filePath);
            }
        }
    }
}
